<template>
  <a-spin :spinning="loading">
  <base-breadcrumb>
    <a-card>
      <a-button class="mb12" type="primary" @click="add" v-if="hideBuquBtn()">新增</a-button>
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :page-keys="['pageNo', 'pageSize', 'totalSize']"
        :show-page="false"
      >
        <div slot="actions" slot-scope="text, record" class="pub-table-list-action">
          <template>
            <span @click="edit(record)" v-if="hideBuquBtn()">编辑</span>
            <span @click="enableFn(record)" v-if="hideBuquBtn()">{{record.status*1===0?'禁用':'启用'}}</span>
            <!-- <span @click="del(record)">删除</span> -->
          </template>
        </div>
      </list-table>
      <updateInspectionTemplate
        :visible.sync="visible"
        :form="form"
        :editModel="editModel"
        @ok="$refs.table.refresh()"
      ></updateInspectionTemplate>
    </a-card>
  </base-breadcrumb>
  </a-spin>
</template>

<script>
import api from '@/api/safetySupervision/base/inspectionTemplate.js'
import updateInspectionTemplate from './updateInspectionTemplate'
const columns = [
  {
    title: '序号',
    width: 50,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    }
  },
  {
    title: '考核项',
    dataIndex: 'checkType',
    width: 180,
  },
  {
    title: '单位',
    dataIndex: 'unit',
    width: 60,
  },
  {
    title: '扣分标准',
    dataIndex: 'deductPoints',
    width: 230,
  },
  {
    title: '选择说明',
    dataIndex: 'content',
    width: 280,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender() {
      return arguments[1].status ? '不可用' : '可用'
    }
  },
  {
    title: '操作',
    dataIndex: 'actions',
    width: 120,
    scopedSlots: { customRender: 'actions' }
  }
]
export default {
  name: 'a' + Date.now(),
  components: {
    updateInspectionTemplate
  },

  data() {
    this.columns = columns
    return {
      form: {},
      visible: false,
      editModel: 1,
      loading:false,
      loadData: async (q) => await api.listBase({})
    }
  },
  methods: {
    del(record) {
      this.$delList3(
        api.delBase,
        (res) => {
          console.log(res)
          this.$refs.table.refresh()
        },
        { id: record.id },
        null
      )
    },
    enableFn(record){
      let msg = record.status*1===1?'启':'禁',
      that = this;
      this.$confirm({
        okText: '确认',
        cancelText: '取消',
        title: '提示?',
        content: `请确认是否要${msg}用?`,
        onOk() {
            that.sureEnable(record,msg);
        },
        onCancel() {
        },
      })
    },
   async sureEnable(str,msg){
       let reqObj = {id:str.id,status:str.status*1===0?1:0};
       this.loading = true;
       let data = await api.changeEnable(reqObj);
       this.loading = false;
       if(data.data.code=='0'){
          this.$message.success(`${msg}用成功！`);
          this.$refs.table.refresh()
       }
    },
    add() {
      this.editModel = 1
      this.form = { status: 0 }
      this.visible = true
    },
    edit(record) {
      this.editModel = 0
      this.form = { ...record }

      this.visible = true
    }
  }
}
</script>